OpenNet <-function () 
{
  file=file.choose()
  Graph=read.graph(file,format="pajek")
  return(Graph)
}

DC_random<-function(File)
{
  G=read.graph(File,format="pajek")
  e=E(G)
  len=length(e)
  for(i in 1:100)
  {
    print(i)
    G1=rewire(G, niter=7*len)
    name=paste(File,"-",i,".net")
    write.graph(G1,name,format="pajek")
  }
}

Knn<-function(File)
{
  Graph=read.graph(File,format="pajek")
  k=graph.knn(Graph)
  Knn=paste(File,"-knn.txt")
  pngg=paste(File,".png")
  png(pngg)
  K=paste(File,"k.txt")
  plot(k$knnk,log="x")
  dev.off()
  write.table(k$knn,Knn)
  write.table(k$knnk,K)
}

Knn_Dir<-function(Dir){
  setwd(Dir)
  files=list.files(Dir)
  for(i in files)
  {
    Knn(i)
  }
}

Print_diff<-function(File)
{
  G=read.graph(File,format="pajek")
  d=degree(G)
  num=floor(0.05*length(V(G)))
  b=betweenness(G,normalized=TRUE)
  c=c()
  name=paste(File,".txt")
  for(i in 1:num)
  {
    d_index=which.max(d)
    b_index=which.max(b)
    c=append(c,c(d_index,b_index))
    d[d_index]=0
    b[b_index]=0
  }
  write.table(c,name)
}

DC_cac<-function(File)
{
  setwd(File)
  files=list.files(File)
  for(i in files)
  {
    DC_random(i)
  }
}

Compare<-function(G)
{
  G$d=degree(G)
  G$b=betweenness(G,normalized=TRUE)
  print("Max degree:")
  print(max(G$d))
  print("Max betweenness:")
  print(max(G$b))
  print("Betweenness at the max degree:")
  print(G$b[which.max(G$d)])
  print("Degree at the max betweenness:")
  print(G$d[which.max(G$b)])
}

Community<-function(file)
{
  setwd(file)
  files=list.files(file)
  for(i in files)
  {
    print(i)
    a=read.graph(i,format="pajek")
    for(j in 1:20)
    {
      name=sprintf("%g",j)
      Lcuname=paste(i,name,"-Label-Cu.txt")
      Icuname=paste(i,name,"-Info-Cu.txt")
      label_cu=label.propagation.community(a)
      info_cu=infomap.community(a)
      write.table(label_cu$membership,Lcuname)
      write.table(info_cu$membership,Icuname)
    }
  }
}

Community2<-function(file)
{
  setwd(file)
  files=list.files(file)
  for(i in files)
  {
    print(i)
    a=read.graph(i,format="pajek")
    Lcuname=paste(i,"-Label-Cu.txt")
    Mname=paste(i,"-M.txt")
    label_cu=label.propagation.community(a)
    write.table(label_cu$membership,Lcuname)
    write.table(label_cu$modularity,Mname)
  }
}

Random<-function(X)
{
  return(sample.int(X,1))
}

Spearman<-function(G)
{
  vect=degree(G)
  rank=cbind(vect,as.numeric(factor(vect)))[,2]
  assortativity(G,rank)
}

PL_cal<-function(file)
{
  g=read.graph(file,format="pajek")
  a=power.law.fit(degree(g))
  return(a)
}

PL<-function(file)
{
  setwd(file)
  files=list.files(file)
  for(i in files)
  {
    a=PL_cal(i)
    fileout=paste(i,"-PL.txt")
    write.table(a,file=fileout)
  }
}

Error<-function(G)
{
  e=E(G)
  for(i in 1:length(e))
  {
    b=delete.edges(G,e[i])
    Spearman(b)
    print(i)
  }
}

Giant<-function(graph)
{
  return(length(cluster.distribution(graph))-1)
}

IsoSize<-function(graph)
{
  c=clusters(graph)$csize
  c=sort(c)
  l=length(c)
  if(l==1)
    return(0)
  else
    return(1.0*(sum(c)-c[l])/(l-1))
}

Judge<-function(File)
{
  Graph=read.graph(File,format="pajek")
  d=degree.distribution(Graph)
  s=d[length(d)]
  delete=s-1.0/length(V(Graph))
  return(delete==0)
}


CascadeRank<-function(Graph,Tolerance,fileG,fileE)
{
  Originalsize=length(V(Graph))
  G=c(1)
  E=c()
  E=append(E,Efficiency(Graph))
  B=betweenness(Graph)
  V(Graph)$OriginalB=2*B/(1.0*Originalsize*Originalsize-3*Originalsize+2)
  V(Graph)$Betweeness=2*B/(1.0*Originalsize*Originalsize-3*Originalsize+2)
  delete=which.max(V(Graph)$Betweeness)
  Graph=Graph-V(Graph)[delete]
  Judge=TRUE
  while(Judge==TRUE)
  {
    Judge=FALSE
    Size=length(V(Graph))
    V(Graph)$Betweeness=betweenness(Graph)*2/(1.0*Size*Size-3*Size+2)
    Delete=(V(Graph)$Betweeness>Tolerance*V(Graph)$OriginalB)
    Graph=Graph-V(Graph)[Delete]
    GiCom=Giant(Graph)
    if(any(Delete==TRUE))
    {Judge=TRUE}
    ratio=Giant(Graph)/Originalsize
    G=append(G,ratio)
  }
  E=append(E,Efficiency(Graph))
  write.table(G,file=fileG)
  write.table(E,file=fileE)
}

CascadeRank2D<-function(Graph,Tolerance,fileG,fileE)
{
  Originalsize=length(V(Graph))
  Norm=1.0*Originalsize*Originalsize-Originalsize
  G=c(1)
  B=betweenness(Graph)
  D=degree(Graph)
  E=c()
  V(Graph)$OriginalB=B
  V(Graph)$Degree=D
  V(Graph)$Betweeness=B
  delete=which.max(V(Graph)$Degree)
  Graph=Graph-V(Graph)[delete]
  Judge=TRUE
  while(Judge==TRUE)
  {
    Judge=FALSE
    Size=length(V(Graph))
    V(Graph)$Betweeness=betweenness(Graph)
    Delete=(V(Graph)$Betweeness>Tolerance*V(Graph)$OriginalB)
    Graph=Graph-V(Graph)[Delete]
    GiCom=Giant(Graph)
    if(any(Delete==TRUE))
    {Judge=TRUE}
    ratio=Giant(Graph)/Originalsize
    G=append(G,ratio)
  }
  E=append(E,1.0*Efficiency_2(Graph)/Norm)
  write.table(G,file=fileG)
  write.table(E,file=fileE)
}

CascadeRank2B<-function(Graph,Tolerance,fileG,fileE)
{
  Originalsize=length(V(Graph))
  Norm=1.0*Originalsize*Originalsize-Originalsize
  G=c(1)
  B=betweenness(Graph)
  D=degree(Graph)
  E=c()
  V(Graph)$OriginalB=B
  V(Graph)$Degree=D
  V(Graph)$Betweeness=B
  delete=which.max(V(Graph)$Betweeness)
  Graph=Graph-V(Graph)[delete]
  Judge=TRUE
  while(Judge==TRUE)
  {
    Judge=FALSE
    Size=length(V(Graph))
    V(Graph)$Betweeness=betweenness(Graph)
    Delete=(V(Graph)$Betweeness>Tolerance*V(Graph)$OriginalB)
    Graph=Graph-V(Graph)[Delete]
    GiCom=Giant(Graph)
    if(any(Delete==TRUE))
    {Judge=TRUE}
    ratio=Giant(Graph)/Originalsize
    G=append(G,ratio)
  }
  E=append(E,1.0*Efficiency_2(Graph)/Norm)
  write.table(G,file=fileG)
  write.table(E,file=fileE)
}

NoRefreshRank<-function()
{
  file=file.choose()
  Graph=read.graph(file,format="pajek")
  Originalsize=length(V(Graph))
  G=c(1)
  V(Graph)$Betweeness=betweenness(Graph,normalized=TRUE)
  result=1
  repeat
  {
    delete=which.max(V(Graph)$Betweeness)
    Graph=Graph-V(Graph)[delete]
    ratio=Giant(Graph)/Originalsize
    if(ratio<0.01&&result==1)
      result=1-length(V(Graph))/Originalsize
    G=append(G,ratio)
    if(length(V(Graph))<=1)
      break
  }
  fileG=paste(file,"-Re-B.txt")
  write.table(result,fileG)
  write.table(G,append=TRUE,fileG)
  
  Graph2=read.graph(file,format="pajek")
  Originalsize=length(V(Graph2))
  G2=c(1)
  result=1
  a=degree(Graph2)
  V(Graph2)$Degree=a
  repeat
  {
    delete=which.max(V(Graph2)$Degree)
    Graph2=Graph2-V(Graph2)[delete]
    ratio=Giant(Graph2)/Originalsize
    if(ratio<0.01&&result==1)
      result=1-length(V(Graph2))/Originalsize
    G2=append(G2,ratio)
    if(length(V(Graph2))<=1)
      break
  }
  fileG=paste(file,"-Re-D.txt")
  write.table(result,fileG)
  write.table(G2,append=TRUE,fileG)
}

NoRefreshRandom2<-function(file)
{
  Graph=read.graph(file,format="pajek")
  Originalsize=length(V(Graph))
  G=c(1)
  S=c(IsoSize(Graph))
  E=c()
  num=0
  repeat
  {
    currentsize=length(V(Graph))
    delete=Random(currentsize)
    Graph=Graph-V(Graph)[delete]
    S=append(S,IsoSize(Graph))
    num=num+1;
    ratio=Giant(Graph)/Originalsize
    G=append(G,ratio)
    if(length(V(Graph))<=1)
      break
  }
  fileG=paste(file,"-Random2-G.txt")
  fileE=paste(file,"-Random2-E.txt")
  fileS=paste(file,"-Random2-S.txt")
  write.table(G,fileG)
  write.table(E,fileE)
  write.table(S,fileS)
}

NoRefreshRank2B<-function(file)
{
  Graph=read.graph(file,format="pajek")
  Originalsize=length(V(Graph))
  norm=1.0*Originalsize*Originalsize-Originalsize
  G=c(1)
  E=c()
  S=c(IsoSize(Graph))
  V(Graph)$Betweeness=betweenness(Graph)
  num=0
  step=floor(Originalsize/100)
  repeat
  {
    delete=which.max(V(Graph)$Betweeness)
    Graph=Graph-V(Graph)[delete]
    Graph=Graph+vertex(1)
    S=append(S,IsoSize(Graph))
	  num=num+1;
#     for(i in 1:100)
#     {
#       if(num==floor(Originalsize/100*i))
#       {
#         E=append(E,1.0*Efficiency_2(Graph)/norm) 
#         print(num)
#         break
#       }
#          
#     }      
    ratio=Giant(Graph)/Originalsize
    G=append(G,ratio)
    if(num==Originalsize)
      break
  }
  fileG=paste(file,"-Re-B-G.txt")
  fileS=paste(file,"-Re-B-S.txt")
  fileE=paste(file,"-Re-B-E.txt")
  write.table(E,fileE)
  write.table(G,fileG)
  write.table(S,fileS)
}

NoRefreshRank2D<-function(file)
{
  Graph=read.graph(file,format="pajek")
  Originalsize=length(V(Graph))
  norm=1.0*Originalsize*Originalsize-Originalsize
  G=c(1)
  E=c()
  S=c(IsoSize(Graph))
  V(Graph)$Degree=degree(Graph)
  step=floor(Originalsize/100)
  num=0
  repeat
  {
    delete=which.max(V(Graph)$Degree)
    Graph=Graph-V(Graph)[delete]
    S=append(S,IsoSize(Graph))
    num=num+1;
    ratio=Giant(Graph)/Originalsize
    G=append(G,ratio)
    for(i in 1:100)
    {
      if(num==floor(Originalsize/100*i))
      {
        E=append(E,1.0*Efficiency_2(Graph)/norm)        
        print(num)
        break
      }
    }  
    if(length(V(Graph))<=1)
      break
  }
  fileG=paste(file,"-Re-D-G.txt")
  fileE=paste(file,"-Re-D-E.txt")
  fileS=paste(file,"-Re-D-S.txt")
  write.table(G,fileG)
  write.table(E,fileE)
  write.table(S,fileS)
}

DeleteHub<-function(file)
{
  Graph=read.graph(file,format="pajek")
  Originalsize=length(V(Graph))
  V(Graph)$Degree=degree(Graph)
  step=floor(Originalsize/100)
  num=0
  repeat
  {
    delete=which.max(V(Graph)$Degree)
    Graph=Graph-V(Graph)[delete] 
    num=num+1
    if(num==step)
      break
  }
  fileG=paste(file,"-Hub.net")
  write.graph(Graph,fileG,format="pajek")
}

RefreshRandom<-function()
{
  file=file.choose()
  Graph=read.graph(file,format="pajek")
  Originalsize=length(V(Graph))
  G=c(1)
  repeat
  {
    currentsize=length(V(Graph))
    delete=Random(currentsize)
    Graph=Graph-V(Graph)[delete]
    ratio=Giant(Graph)/Originalsize
    G=append(G,ratio)
    if(length(V(Graph))<=1)
      break
  }
  fileG=paste(file,"-Refresh-Random-G.txt")
  write.table(G,fileG)
}

Cascade<-function()
{
  file=file.choose()
  Graph=read.graph(file,format="pajek")
  
  for(i in 1:5)
  {
    Tolerance=1+i/10
    ch=sprintf("%g",Tolerance)
    fileG=paste(file,ch,"Cascade-Rank-G.txt")
    fileE=paste(file,ch,"Cascade-Rank-E.txt")
    CascadeRank(Graph,Tolerance,fileG,fileE)
  }
  for(i in 1:1)
  {
    Tolerance=1+i/10
    ch=sprintf("%g",Tolerance)
    fileG=paste(file,ch,"Cascade-Random-G.txt")
    fileE=paste(file,ch,"Cascade-Random-E.txt")
    CascadeRondom(Graph,Tolerance,fileG,fileE)
  }
}

Cascade2<-function(file)
{
  Graph=read.graph(file,format="pajek")
  for(i in 1:5)
  {
    Tolerance=1+i/10
    ch=sprintf("%g",Tolerance)
    fileDG=paste(file,ch,"Cascade-D-Rank2-G.txt")
    fileDE=paste(file,ch,"Cascade-D-Rank2-E.txt")
    fileBG=paste(file,ch,"Cascade-B-Rank2-G.txt")
    fileBE=paste(file,ch,"Cascade-B-Rank2-E.txt")
    CascadeRank2D(Graph,Tolerance,fileDG,fileDE)
    CascadeRank2B(Graph,Tolerance,fileBG,fileBE)
  }
}

Refresh_of_All<-function(File)
{
	setwd(File)
	files=list.files(File)
	for(fileo in files)
	{
		NoRefreshRank2B(fileo)
#NoRefreshRank2D(fileo)
# 		Cascade2(fileo)
	}
}


Random_of_All<-function()
{
  setwd("D:/Random/")
  files=list.files("D:/Random/")
  for(fileo in files)
  {
    NoRefreshRandom2(fileo)
  }
}

Random_of_All<-function()
{
  setwd("E:/Experiment/Result/Net/")
  files=list.files("E:/Experiment/Result/Net/")
  for(fileo in files)
  {
    Graph=read.graph(fileo,format="pajek")
  }
}

Cascades<-function()
{
  setwd("D:/Refresh1/")
  files=list.files("D:/Refresh1/")
  for(fileo in files)
  {
    Cascade2(fileo)
  }
}

Efficiency<-function(Graph)
{
  size=length(V(Graph))
  E=0
  for(i in 1:size)
  {
    t=shortest.paths(Graph,v=V(Graph)[i],algorithm="dijkstra")
    E=E+sum(1/t[t!=0])
  }
  return(E/size/(size-1))
  
}

Efficiency_2<-function(Graph)
{
  return (1)
  size=length(V(Graph))
  E=0
  for(i in 1:size)
  {
    t=shortest.paths(Graph,v=V(Graph)[i],algorithm="dijkstra")
    E=E+sum(1/t[t!=0])
  }
  return(E)
  
}

EfficiencyE_B<-function(Graph)
{
  E=Refresh_D()
  size=length(V(Graph))
  for(i in 1:size)
  {
    path=get.shortest.paths(Graph,V(Graph)[i])
    for(n in 1:size)efficiency()
{
      if(n==i)
      {next}
      t=length(path[n])
      E=E+1/t
    }
  }
return(E/size/(size-1))
}

Q<-function()
{
  file=file.choose()
  Graph=read.graph(file,format="pajek")
  d=degree(Graph) 
  r=gpd.fit(d,method="amle")
  shape=r[1]
  q=(2*r[1]+1)/(r[1]+1)
  fileout=paste(file,"-q.txt")
  write.table(r,fileout,append=TRUE)
  write.table(q,fileout,append=TRUE)
}

degree1_2<-function(Graph)
{
  degree=degree.distribution(Graph)
  a=0
  b=0
  for(i in 1:length(degree))
  {
    a=a+i*degree[i]
    b=b+i*i*degree[i]
  }
  return(1.0*b/a)
}
